package com.example.NearByLocation;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by lenovo on 2014/11/26.
 */
public class LocationManager {

    private LocationClient locationClient = null;

    private BDLocationListener bdLocationListener = null;

    private Context context;

    private TextView textView;

    private ProgressDialog dialog;

    public static   LatLng currentLat;
    public static String add;

    public LocationManager(Context context,TextView textView){
        this.context = context;
        this.textView = textView;
    }
    public LocationManager(){

    }

    public void getCurretLocation(Context context){
        textView.setText("定位中，请稍等....");
        dialog = new ProgressDialog(context);
        dialog.setMessage("定位中，请稍等。。。。。");
        dialog.show();
        locationClient = new LocationClient(context);
        bdLocationListener = new MyLocationListener();
        locationClient.registerLocationListener(bdLocationListener);
        setLocationOption();
        locationClient.start();
        locationClient.requestLocation();


    }

    private void setLocationOption(){
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setIsNeedAddress(true);//返回的定位结果包含地址信息
        option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
//        option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
        locationClient.setLocOption(option);

    }


    private class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null) {
                return;
            }
            currentLat=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
            add = bdLocation.getAddrStr();

            Log.d("lat",bdLocation.getLatitude()+"");
            Log.d("lon",bdLocation.getLongitude()+"");
            textView.setText(add);
            dialog.dismiss();
        }
    }
    public void stopLocation() {
        if (locationClient != null || locationClient.isStarted()) {
            locationClient.stop();

        }
    }
    public LatLng getAdd(){

        return currentLat;
    }
    public String getAddName(){

        return add;
    }
}
